package strategies;

import utils.EditorContext;
import utils.Title;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ListTreeStrategy implements DisplayStrategy {
    protected EditorContext context;
    protected List<Title> titleList = new ArrayList<>();

    public ListTreeStrategy(EditorContext context) {
        this.context = context;
        int[] currentLevel = {0};
        Arrays.asList(context.getContent().split("\n")).forEach(str -> {
            int level = countLeadingHashes(str);
            if (level > 0) { // 只有当 level > 0 时才视为标题
                String text = str.substring(level + 1).trim(); // 从 level+1 开始，跳过空格
                titleList.add(new Title(text, level, null, 0));
                currentLevel[0] = level;
            } else if (str.startsWith("*")) {
                String text = str.trim();
                titleList.add(new Title(text, currentLevel[0] + 1, null, 0));
            }
        });
    }

    @Override
    public void display() {
        findParentAndPosition();
        printTitleWithIndent();
    }

    protected static int countLeadingHashes(String line) {
        int count = 0;
        while (count < line.length() && line.charAt(count) == '#') {
            count++;
        }
        // 确保 # 后面有空格
        if (count < line.length() && line.charAt(count) == ' ') {
            return count;
        } else {
            return 0; // 如果没有空格，则不是标题，返回0
        }
    }

    protected void findParentAndPosition() {
        int n = titleList.size();
        for (int i = 0; i < n; i++) {
            int j, k;
            //取i左边第一个比i小一级的标题，作为i的父标题
            for (j = i - 1; j >= 0; j--) {
                if (titleList.get(i).getLevel() - titleList.get(j).getLevel() == 1) {
                    titleList.get(i).setParent(titleList.get(j));
                    break;
                }
            }
            //看i是不是父标题下最后一个同级标题
            boolean isLast = true;
            for (k = i + 1; k < n; k++) {
                if (titleList.get(i).getLevel() - titleList.get(k).getLevel() == 1) {
                    break;
                }
            }
            for (int l = i + 1; l < k; l++) {
                if (titleList.get(l).getLevel() == titleList.get(i).getLevel()) {
                    isLast = false;
                    break;
                }
            }
            if (isLast) {
                titleList.get(i).setPosition(2);
            } else {
                titleList.get(i).setPosition(1);
            }
        }
    }

    protected void printTitleWithIndent() {
        for (Title title : titleList) {
            StringBuilder result = new StringBuilder(title.getText());
            if (title.getText().startsWith("*")) {
                result = new StringBuilder("·" + title.getText().substring(1).trim()); // 修改列表项的前缀
            }
            if (title.getPosition() == 2) {
                result.insert(0, "└── ");
            } else {
                result.insert(0, "├── ");
            }
            for (int level = title.getLevel() - 1; level > 0 && title.getParent() != null; level--) {
                if (title.getParent().getPosition() == 2) {
                    result.insert(0, "\t");
                } else {
                    result.insert(0, "│\t");
                }
                title = title.getParent();
            }
            System.out.println(result);
        }
    }
}
